package net.lainiao.morethread.Chapter4.demo10;

import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/3.
 */
public class ReportProcess implements Runnable {
    private CompletionService<String> service;
    private boolean end;

    public ReportProcess(CompletionService<String> service) {
        this.service = service;
        end = false;
    }

    @Override
    public void run() {
       while (!end){
           try {
               Future<String> result = service.poll(20, TimeUnit.SECONDS);
               if(result!=null){
                   String report=result.get();
                   System.out.println("ReportReceiver:Report Received:"+report);
               }
           } catch (InterruptedException e) {
               e.printStackTrace();
           } catch (ExecutionException e) {
               e.printStackTrace();
           }
       }
        System.out.println("ReportSender:End");
    }

    public void setEnd(boolean end) {
        this.end = end;
    }
}
